package com.leave.web.servlet;

import com.leave.context.AppContext;
import com.leave.service.AdminService;
import com.leave.service.InstructorService;
import com.leave.service.StudentService;
import com.leave.tools.MD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * 基于页面控制器模式的登录验证
 * @author amu_1115@126.com
 * @version 1.0
 */
@WebServlet(name = "loginServlet",urlPatterns = {"/login"})
public class LoginServlet extends BaseHttpServlet {
    private final static String  VIEW_MAIN="student_index.jsp";
    private final static String  DEPART_LIST = "department_list.jsp";
    private final static String  VIEW_LOGIN="login.jsp";
    //业务组件-
    private StudentService userService;
    private AdminService adminService;
    private InstructorService instructorService;

    @Override
    public void init() throws ServletException {
        super.init();
         //从IoC容器获取实例化bean组件
         userService= AppContext.getBean(StudentService.class);
         adminService=AppContext.getBean(AdminService.class);
         instructorService = AppContext.getBean(InstructorService.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1:接收参数
         String id =req.getParameter("id");
         String password =req.getParameter("password");
         String t1=req.getParameter("t1");
        //2:调用service组件
         try{
             id = StringUtils.trim(id);
             password = StringUtils.trim(password);
             password = MD5Util.encryptMd5(password);
             if("1".equals(t1)){
                 boolean flag = userService.checkStudent(id,password);
//                 System.out.println("flag="+flag);
                 if (flag){
                     super.doDispatch(req,resp,"leave?action=list");
                 }else{
                     super.doDispatch(req,resp,VIEW_LOGIN);
                 }
             }else if ("2".equals(t1)){
                 boolean flag = instructorService.checkInstructor(id,password);
                 if (flag){
                     super.doDispatch(req,resp,"classes?action=list");
                 }else{
                     super.doDispatch(req,resp,VIEW_LOGIN);
                 }
             } else if ("3".equals(t1)){
                 boolean flag=adminService.check(id,password);
                 if (flag){
//                     System.out.println(3);
                     super.doDispatch(req,resp,"admin?action=adminList");
                 }else{
                     super.doDispatch(req,resp,VIEW_LOGIN);
                 }
             }else {
                 super.doDispatch(req,resp,VIEW_LOGIN);
             }
             //验证用户+密码的合法性
         }catch (Exception e){
             e.printStackTrace();
              super.doRedirect(req,resp,req.getContextPath()+ErrorPage_500);
         }

    }
}
